package backEnd;

public class Hint {

	String currentHint;	   // Current hint of either player
	String rawCurrentHint; // Same as currentHint w/o numbers. Used for validation
	private Integer MIN;
	private Integer MAX;
	private Integer NUM;
	private Integer LIMIT;

        // List of hints for multiplication type of game
	String[] hintMul = { "The answer is ODD",
			     "The answer is EVEN",
			     "The answer is between MIN and MAX, inclusive",
			     "The answer contains a NUM",
			     "The answer is less than LIMIT" };

        // List of hints for addition type of game
        String[] hintAdd = { "The answer is ODD",
			   "The answer is EVEN",
                           "The answer contains a NUM",
                           "The answer is greater than LIMIT"};

	/**
	 * Function to retrieve the current hint
	 * @return Current Hint
	 */
	public String getNextHint() {
		return currentHint;
	}

	/**
	 * Function to set the a new random hint
	 */
	public void setNextHint(String gameType) {
		String temp = String.valueOf(System.currentTimeMillis());
                System.out.println(temp);
		Integer tempindex = Integer.parseInt(temp.substring(temp.length() - 2));
                NUM = (Integer.parseInt(temp.substring(temp.length() - 2)) % 10);
                if(gameType.equals("Multiplication")){
                    int index = tempindex % hintMul.length;
                    currentHint = hintMul[index];
                    rawCurrentHint = hintMul[index];

                    MIN = Integer.parseInt(temp.substring(temp.length() - 3)) % 51;
                    MAX = MIN + (Integer.parseInt(temp.substring(temp.length() - 3)) % 20)
                                    + 10;
                    LIMIT = (Integer.parseInt(temp.substring(temp.length() - 3)) % 61) + 20;
                    currentHint = replaceString(currentHint, "MIN", MIN.toString());
		    currentHint = replaceString(currentHint, "MAX", MAX.toString());
                    currentHint = replaceString(currentHint, "LIMIT", LIMIT.toString());
                    currentHint = replaceString(currentHint, "NUM", NUM.toString());
                }
                if(gameType.equals("Addition")){
                    int index = tempindex % hintAdd.length;
                    currentHint = hintAdd[index];
                    rawCurrentHint = hintAdd[index];

                    LIMIT = (Integer.parseInt(temp.substring(temp.length() - 3)) % 9) + 3;
                    currentHint = replaceString(currentHint, "LIMIT", LIMIT.toString());
                    currentHint = replaceString(currentHint, "NUM", NUM.toString());
                }
	}

	/**
	 * Utility function called from within the class
	 * Replaces 'target' in the 'fromString' with the value 'replacement'
	 * @param fromString - Main string in which replacement is required
	 * @param target - What needs to be replaced
	 * @param replacement - The replacement string
	 * @return - New string after replacement
	 */
	private static String replaceString(String fromString, String target, String replacement){
		int point = fromString.indexOf(target);
		if(point> -1){
			String first =  fromString.substring(0, point);
			String last = fromString.substring(point + target.length());
			return first + replacement + last;
		}
		return fromString;
	}

	/**
	 * Validation function to evaluate if the game move was right or wrong
	 * @param gameType - Multiplication or Addition
	 * @param row
	 * @param cell
	 * @return - Number if the move is valid, or '??' otherwise
	 */
	public String evaluate(String gameType, int row, int cell) {
		String returnValue = null;

		if (gameType.equals("Multiplication")) {
			if (rawCurrentHint.contains("ODD")) {
				returnValue = (((row * cell) % 2) == 1) ? (row * cell) + ""
						: "??";
			} else if (rawCurrentHint.contains("EVEN")) {
				returnValue = (((row * cell) % 2) == 0) ? (row * cell) + ""
						: "??";
			} else if (rawCurrentHint.contains("MIN")) {
				returnValue = ((row * cell) >= MIN && (row * cell) <= MAX) ? (row * cell)
						+ ""
						: "??";
			} else if (rawCurrentHint.contains("NUM")) {
				returnValue = (String.valueOf(row * cell).contains(NUM
						.toString())) ? (row * cell) + "" : "??";
			} else if (rawCurrentHint.contains("LIMIT")) {
				returnValue = row * cell < LIMIT ? (row * cell) + "" : "??";
			}
		}

		if (gameType.equals("Addition")) {
			if (rawCurrentHint.contains("ODD")) {
				returnValue = (((row + cell) % 2) == 1) ? (row + cell) + ""
						: "??";
			} else if (rawCurrentHint.contains("EVEN")) {
				returnValue = (((row + cell) % 2) == 0) ? (row + cell) + ""
						: "??";
			} else if (rawCurrentHint.contains("NUM")) {
				returnValue = (String.valueOf(row + cell).contains(NUM
						.toString())) ? (row + cell) + "" : "??";
			} else if (rawCurrentHint.contains("LIMIT")) {
				returnValue = row + cell < LIMIT ? (row + cell) + "" : "??";
			}
		}

		//this.setNextHint();
		return returnValue;
        }
}
